﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Net;
using System.Security.Principal;
using System.Runtime.InteropServices;


public partial class _Default : System.Web.UI.Page
{
    private zhixin.MYDNS DnsServer = new zhixin.MYDNS();
    protected void Page_Load(object sender, EventArgs e)
    {
        DnsServer.ServerName = "202.96.64.68";
        DnsServer.userName = "Administrator";
        DnsServer.passWord = "KASS117waner!";
        string ContainerName = ConfigurationManager.AppSettings["MainUrl"].ToString();
        Response.Write(ContainerName);
        if (Request["IP"] != null)
        {
            Response.Write(Request["IP"]);
            Dictionary<UInt32, string> DnsRecord = GetDNSRecord(ContainerName);
            if (DnsRecord != null)
            {
                IPAddress address;
                string strIP = Request["IP"];
                if (IPAddress.TryParse(strIP, out address))
                {
                    UInt32 IP = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
                    string HostName = GetHostName(IP);
                    if (DnsRecord.ContainsKey(IP))
                    {
                        Response.Write(DnsRecord[IP]);
                    }
                    else
                    {
                        for (int i = 1; i < 10; i++)
                        {
                            if (DnsServer.CreateAType(ContainerName, HostName, strIP, "6000"))
                            {
                                DnsRecord.Add(IP, HostName + "." + ContainerName);
                                Response.Write(HostName + "." + ContainerName);
                                break;
                            }
                            HostName = GetHostName(IP, i);
                        }

                    }
                }
            }
        }
        Response.End();

        //MyDnsTEST();

    }

    private string GetHostName(UInt32 address)
    {
        return address.ToString();
    }
    private string GetHostName(UInt32 address, int key)
    {
        return address.ToString() + key.ToString();
    }
    private Dictionary<UInt32, string> GetDNSRecord(string ContainerName)
    {
        object Record = Application[ContainerName];
        if (Record != null)
        {
            try
            {
                Dictionary<UInt32, string> DNSRecord = (Dictionary<UInt32, string>)Record;
                if (DNSRecord.Count > 0)
                    return DNSRecord;
                else
                    Application[ContainerName] = null;

            }
            catch (Exception)
            {
            }

        }
        if (DnsServer.IsExistsZone(ContainerName))
        {
            DataTable Table = DnsServer.ListExistsAType(ContainerName);
            Dictionary<UInt32, string> DnsRecord = new Dictionary<UInt32, string>();
            foreach (DataRow row in Table.Rows)
            {
                IPAddress address;
                if (IPAddress.TryParse(row["IPAddress"].ToString(), out address))
                {
                    UInt32 IP = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
                    if (!DnsRecord.ContainsKey(IP))
                        DnsRecord.Add(IP, row["HostName"].ToString());
                }
            }
            Application[ContainerName] = DnsRecord;
            return DnsRecord;
        }
        return null;

    }
    /// 
    /// MyDnsTEST功能测试
    /// C#利用WMI操作DNS服务器(可远程操作,需要相应权限)
    /// Author:yaosansi 
    /// Create Date:2005-09-07
    /// Modify Date:2006-10-25
    /// Site:http://www.yaosansi.com/
    /// E-mail:yaosansi at 126 dot com
    /// http://www.yaosansi.com/blog/article.asp?id=935
    /// http://yaosansi.cnblogs.com/archive/2006/11/04/DNSServiceInCsharpWithWMI.html
    /// 注意:此版本为WINDOWS2003 DNS服务器专用.不适合其它版本操作系统.
    /// 
    static void MyDnsTEST()
    {
        zhixin.MYDNS dns = new zhixin.MYDNS();
        //===========================================
        //不对以下三个属性赋值默认DNS服务器为本机.
        dns.ServerName = "220.179.115.117";
        dns.userName = "Administrator";
        dns.passWord = "KASS117waner!";
        //===========================================
        //dns.CreateZone("dns1.golib.net");
        //dns.DelZone("yaosansi.com");
        //dns.CreateAType("dns1.golib.net", "www", "2.2.2.2", "3600");
        //dns.ModifyAType("yaosansi.com", "www", "127.21.0.1", "800");
        //dns.DelAType("yaosansi.com", "mail");
        //dns.CreateMXType("mail", "yaosansi.com", "5.5.5.5", "20", "3600");
        //dns.ModifyMXType("mail", "yaosansi.com", "36000", "218.1.1.1", "26");
        //dns.DelMXType("mail", "yaosansi.com");
        //dns.CreateCNAMEType("mpq2", "yaosansi.com", "www.yaosansi.com", "3900");
        //dns.ModifyCNAMEType("mpq2", "abc.com", "30520", "www.yaosansi.com.");
        //dns.DelCNAMEType("mpq", "yaosansi.com");

        //DataTable table = dns.ListExistsMXType("yaosansi.com");
        DataTable table = dns.ListExistsAType("dns1.golib.net");
        //DataTable table = dns.ListExistsCNAMEType("yaosansi.com");
        //zhixin.Data.DataHelp.PrintTable(table);
    System.Web.HttpContext.Current.Response.Write (table.Rows.Count+"");

        if (!string.IsNullOrEmpty(dns.ErrMessage))
        {
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine("返回信息:" + dns.ErrMessage);
            Console.WriteLine("--------------------------------------------------");
        }
        Console.WriteLine("");
        Console.WriteLine("===End===");
        Console.ReadLine();
    }


}
